package ru.incoremedia.gwttest.core.server.util;

import com.google.appengine.api.datastore.Key;
import java.io.Serializable;
import ru.incoremedia.gaeutils.model.BaseEntity;
import ru.incoremedia.gwttest.core.client.dto.EntityDto;
import ru.incoremedia.gwttest.core.client.dto.KeyDto;

public class DtoUtils {

    public static void copyProperties(BaseEntity baseEntity, EntityDto entityDto) {
        for(Enum property : entityDto.getPropertiesEnum()) {
            if(baseEntity.hasProperty(property)) {

                Serializable value = baseEntity.getProperty(property);

                if(value instanceof Key) {
                    Key key = (Key)value;
                    KeyDto keyDto = new KeyDto();
                    keyDto.setId(key.getId());
                    keyDto.setName(key.getName());
                    keyDto.setKind(key.getKind());
                    value = keyDto;
                }

                entityDto.setProperty(property, value);
            }
        }
    }

}
